package eginterface;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GaoBo
 * Date: 2021-05-23
 * Time: 9:17
 */
/*class Person implements Comparable<Person>{
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //比较规则  谁调用compareTo 谁就是this
    @Override
    public int compareTo(Person o) {
        *//*if(this.age > o.age) {
            return 1;
        }else if(this.age == o.age) {
            return 0;
        }else {
            return -1;
        }*//*
        //return this.age - o.age;
        return this.name.compareTo(o.name);
    }
}*/
class Money implements Cloneable{
    public double money=9.9;


    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable{
    public String name;
    public int age;
    public Money m = new Money();


    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //return super.clone();
        Person p = (Person) super.clone();
        p.m = (Money) this.m.clone();
        return p;
    }

}


public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person("bit",12);

        Person person2 = (Person) person.clone();

        System.out.println(person.m.money);//9.9
        System.out.println(person2.m.money);//9.9

        System.out.println("===========修改==============");
        person2.m.money = 99999.99;
        System.out.println(person.m.money);
        System.out.println(person2.m.money);

    }

    public static void main5(String[] args) throws CloneNotSupportedException {
        Person person = new Person("bit",12);

        Person person2 = (Person) person.clone();

        System.out.println(person2);
    }

    public static void main4(String[] args) {
        Person[] people = new Person[3];
        people[0] = new Person("bit",12);
        people[1] = new Person("ait",34);
        people[2] = new Person("gaobo",18);

        //AgeComparator c = new AgeComparator();
        NameComparator c = new NameComparator();
        Arrays.sort(people,c);

        System.out.println(Arrays.toString(people));
    }

    public static void main3(String[] args) {

        Person  person1 = new Person("git",14);

        Person  person2 = new Person("bit",14);

        AgeComparator ageComparator = new AgeComparator();

        int ret = ageComparator.compare(person1,person2);

        System.out.println(ret);

        System.out.println("==========按姓名比较==========");

        NameComparator nameComparator = new NameComparator();

        System.out.println(nameComparator.compare(person1, person2));
    }

    public static void main2(String[] args) {
        Person[] people = new Person[3];
        people[0] = new Person("bit",12);
        people[1] = new Person("ait",34);
        people[2] = new Person("gaobo",18);
        Arrays.sort(people);
        System.out.println(Arrays.toString(people));


      /*  if(people[0].compareTo(people[1]) > 0) {

        }*/


        //Comparable comparable = new Person("123",12);

    }

    /**
     * 如果自己定义的类型要进行比较，一定要实现接口
     *    1.Comparable  -》 compareTo
     * @param args
     */
    public static void main1(String[] args) {
        Person  person1 = new Person("ait",14);
        Person  person2 = new Person("bit",14);
/*        int ret = person1.compareTo(person2);
        System.out.println(ret);*/
    }

}
